package com.sbw.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.sbw.wx.entity.usermanager.UserListReq;
import com.sbw.wx.path.RequestPathConst;
import com.sbw.wx.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;

/**
 * Desc: 用户管理
 * @Date: 2021/6/28 21:27
 * @Author: Lee
 * 面朝大海  春暖花开
 * */
@Slf4j
@RestController
@RequestMapping("/userManager")
public class UserManagerController {

    @Autowired
    private AccessTokenUtil accessTokenUtil;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * Desc: 获取用户列表
     * @Date: 2021/6/28 21:28
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @GetMapping("/getUserList")
    public String getUserList(@RequestParam("next_openid") String nextOpenid){
        log.info("获取用户列表开始");
        String requestUrl = MessageFormat.format(RequestPathConst.getUserList, accessTokenUtil.getAccessToken(), nextOpenid);
        log.info("获取用户列表请求Url:{}", requestUrl);
        ResponseEntity<String> responseEntityStr = restTemplate.getForEntity(requestUrl, String.class);
        log.info("获取用户列表响应:{}", responseEntityStr.getBody());
        JSONObject retObj = JSONObject.parseObject(responseEntityStr.getBody());
        if (StringUtils.isEmpty(retObj.getString("errcode"))) {
            log.info("获取用户列表返回结果是:{}", retObj.toString());
            return retObj.toString();
        }
        log.error("获取用户列表异常，异常Code:{}, 异常信息:{}", retObj.get("errcode"), retObj.getString("errmsg"));
        return "";
    }

    /**
     * Desc: 获取用户基本信息
     * @Date: 2021/6/28 21:44
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @GetMapping("/getUserInfo/{openId}")
    public String getUserInfo(@PathVariable("openId") String openId){
        log.info("获取用户基本信息开始");
        String requestUrl = MessageFormat.format(RequestPathConst.getUserInfo, accessTokenUtil.getAccessToken(), openId);
        log.info("获取用户基本信息请求Url:{}", requestUrl);
        ResponseEntity<String> responseEntityStr = restTemplate.getForEntity(requestUrl, String.class);
        log.info("获取用户基本信息响应:{}", responseEntityStr.getBody());
        JSONObject retObj = JSONObject.parseObject(responseEntityStr.getBody());
        if (StringUtils.isEmpty(retObj.getString("errcode"))) {
            log.info("获取用户基本信息结果是:{}", retObj.toString());
            return retObj.toString();
        }
        log.error("获取用户基本信息异常，异常Code:{}, 异常信息:{}", retObj.get("errcode"), retObj.getString("errmsg"));
        return "";
    }

    /**
     * Desc: 批量获取用户信息
     * @Date: 2021/6/28 21:46
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @PostMapping("/batchGetUserInfo")
    public String batchGetUserInfo(@RequestBody UserListReq userListReq){
        log.info("批量获取用户基本信息开始，方法入参:{}", userListReq.toString());
        String requestUrl = MessageFormat.format(RequestPathConst.batchGetUserList, accessTokenUtil.getAccessToken());
        log.info("批量获取用户基本信息请求Url:{}", requestUrl);
        String requestJson = JSONObject.toJSONString(userListReq);
        log.info("批量获取用户基本信息请求参数:{}", requestJson);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity(requestJson, headers);
        ResponseEntity<String> responseEntityStr = restTemplate.postForEntity(requestUrl, request, String.class);
        log.info("批量获取用户基本信息响应:{}", responseEntityStr.getBody());
        JSONObject retObj = JSONObject.parseObject(responseEntityStr.getBody());
        if (StringUtils.isEmpty(retObj.getString("errcode"))) {
            log.info("批量获取用户基本信息结果是:{}", retObj.toString());
            return retObj.toString();
        }
        log.error("批量获取用户基本信息异常，异常Code:{}, 异常信息:{}", retObj.get("errcode"), retObj.getString("errmsg"));
        return "";
    }

}
